package com.example.socketdemo.buffer.three;

import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.Scanner;

/**
 * bio 客户端
 */
public class ClientSocket {

    public static void main(String[] args) {
        Socket socket = null;
        try {
            // 连接服务器socket
            socket = new Socket("127.0.0.1", 9999);
            // 从Socket通信管道中得到一个字节输出流。
            OutputStream outputStream = socket.getOutputStream();
            // 把字节流改装成自己需要的流进行数据的发送
            PrintWriter pw = new PrintWriter(outputStream);

            // 开始发送消息
            //pw.println("hello");

            Scanner sc = new Scanner(System.in);
            while (true){
                System.out.print("请说");
                String s = sc.nextLine();
                pw.println(s);
                pw.flush();
            }

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if(socket != null){
                    socket.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }

        }


    }
}
